<template>
  <div class="event-bus-sibling-a">
    <h4>事件总线兄弟组件 A</h4>
    <p>从兄弟B接收的消息: {{ receivedMessage }}</p>
    
    <div>
      <input 
        v-model="newMessage" 
        placeholder="输入消息发送给兄弟B"
      />
      <button @click="sendMessage">发送消息</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import emitter from '../../utils/eventBus.js'

const newMessage = ref('')
const receivedMessage = ref('')

// 发送消息
const sendMessage = () => {
  emitter.emit('message-from-a', newMessage.value)
  newMessage.value = ''
}

// 监听消息
const handleMessageFromB = (message) => {
  receivedMessage.value = message
}

onMounted(() => {
  emitter.on('message-from-b', handleMessageFromB)
})

onUnmounted(() => {
  emitter.off('message-from-b', handleMessageFromB)
})
</script>

<style scoped>
.event-bus-sibling-a {
  padding: 15px;
  background-color: #e6f7ff;
  border-radius: 6px;
  margin-bottom: 15px;
}

.event-bus-sibling-a div {
  margin: 10px 0;
}

.event-bus-sibling-a input {
  padding: 8px;
  margin-right: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  width: 70%;
}

.event-bus-sibling-a button {
  padding: 8px 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.event-bus-sibling-a button:hover {
  background-color: #40a9ff;
}
</style>